﻿
Ext.namespace('FantastPlatform', 'FantastPlatform.UI');
FantastPlatform.UI.PoliceStreetForm = function (config) {
    Ext.apply(this, config);
    var orgStore = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
            url: '../Service/WorkLog.aspx?Action=GetOrganization'
        }),
        reader: new Ext.data.JsonReader({
            root: 'topics',
            totalProperty: 'totalCount',
            id: 'Name',
            fields: ['Id', 'Name']
        }),
        remoteStore: true
    });
    orgStore.load();
    var policeStore = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
            url: '../Service/WorkLog.aspx?Action=GetPoliceByOrgId'
        }),
        reader: new Ext.data.JsonReader({
            root: 'topics',
            totalProperty: 'totalCount',
            id: 'Name',
            fields: ['Id', 'Name']
        }),
        remoteStore: true
    });
    policeStore.load();
    var streetStore = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
            url: "../Service/ResponsibilityAreaStreet.aspx?Action=GetStreet"
        }),
        reader: new Ext.data.JsonReader({
            root: 'topics',
            totalProperty: 'totalCount',
            id: 'Name',
            fields: ['Id', 'Name']
        }),
        remoteStore: true
    });
    streetStore.load();
    var orgComboBox = new Ext.form.ComboBox({
        fieldLabel: '<font color="red">*</font>&nbsp;&nbsp;部门',
        name: 'Organization',
        hiddenName: 'OrganizationId',
        store: orgStore,
        displayField: 'Name',
        mode: 'remote',
        valueField: 'Id',
        allowBlank: false,
        editable: false,
        triggerAction: 'all',
        emptyText: '选择部门组',
        readOnly: true,
        width: 250
    });

    var policeComboBox = new Ext.form.ComboBox({
        fieldLabel: '警员名称',
        name: 'PoliceMan',
        hiddenName: 'PoliceMan',
        store: policeStore,
        displayField: 'Name',
        mode: 'remote',
        valueField: 'Id',
        allowBlank: false,
        editable: false,
        triggerAction: 'all',
        emptyText: '选择警员',
        readOnly: true,
        width: 250
    });
    this.PoliceStore = policeStore;
    this.OrgStore = orgStore;

    orgComboBox.on('select', function () {
        var Id = orgComboBox.getValue();
        var url = '../Service/WorkLog.aspx?Action=GetPoliceByOrgId';
        policeStore.removeAll();
        policeStore.proxy.conn = { method: 'post', url: url };
        policeStore.reload({ params: { Id: Id} });
        var url1 = '../Service/HouseDistrictService.aspx?Action=GetStreet';
        streetStore.removeAll();
        streetStore.proxy.conn = { method: 'post', url: url1 };
        streetStore.reload({ params: { Id: Id} });
    });
    this.items = [{
        layout: 'column',
        border: false,
        labelSeparator: '：',
        items: [{
            columnWidth: 1,
            layout: 'form',
            border: false,
            items: [orgComboBox]
        }, {
            columnWidth: 1,
            layout: 'form',
            border: false,
            items: [policeComboBox]
        }, {
            columnWidth: 1,
            border: false,
            layout: 'form',
            items: [new FantastPlatform.CaseManagement.SearchField({
                fieldLabel: '街道名称',
                name: 'StreetName',
                hiddenName: 'StreetId',
                valueField: 'Id',
                parentField: 'OrganizationId',
                url: '../Service/HouseDistrictService.aspx?Action=GetStreet',
                readOnly: true,
                allowBlank: false,
                blankText: '所属单位不能为空',
                width: 250,
                height:20
            })]
        }]
    }];

    FantastPlatform.UI.PoliceStreetForm.superclass.constructor.call(this, {
        bodyStyle: 'padding:10px',
        collapsible: true,
        labelAlign: 'right',
        autoScroll: true,
        border: false
    });
};

Ext.extend(FantastPlatform.UI.PoliceStreetForm, Ext.form.FormPanel, {
    onRender: function (ct, position) {
        FantastPlatform.UI.PoliceStreetForm.superclass.onRender.call(this, ct, position);
        if (this.Id && this.Id != 0) {
            var Id = this.Id;
            this.add(new Ext.form.Hidden({ name: 'Id', value: Id }));
            var streetform = this.form;
            var pStore = this.PoliceStore
            var url2 = '../Service/WorkLog.aspx?Action=GetOrganization';
            this.OrgStore.removeAll();
            this.OrgStore.proxy.conn = { method: 'post', url: url2 };
            this.OrgStore.load({
                callback: function (form, action) {
                    var url = '../Service/WorkLog.aspx?Action=GetPoliceByOrgId';
                    pStore.removeAll();
                    pStore.proxy.conn = { method: 'post', url: url };
                    pStore.load({ callback: function (form, action) {
                        streetform.load({
                            url: '../Service/ResponsibilityAreaStreet.aspx?Action=Get',
                            params: { id: Id },
                            waitMsg: '正在读取数据...',
                            waitTitle: '请稍后...',
                            failure: function (form, action) {
                                if (action.failureType != 'client') {
                                    showErrorMessage('操作失败', action.result.message, '');
                                }
                            }

                        });

                    }
                    });
                }
            });
        }
    }
});

